/**
* @program: src
*
* @description:
*
* @author: Mr.chen
*
* @create: 2022-04-11 11:39
**/
package config

import (
	"do_demo/srv/common/core/logx"
	"do_demo/srv/common/core/prometheus"
	"do_demo/srv/common/core/trace"
	"do_demo/srv/common/service"
	xtime "do_demo/srv/common/time"
	"flag"
	"github.com/BurntSushi/toml"
	log "github.com/golang/glog"
	"time"
)
var (
	confPath  string
	// Conf config
	Conf *Config
)

func init() {
	flag.StringVar(&confPath, "conf", "etc/logic-example.toml", "default config path")
}

// Init init config.
func Init() (err error) {
	Conf = Default()
	log.Infof("testsrv-logic [version: %+v] conf", Conf)
	_, err = toml.DecodeFile(confPath, &Conf)
	return
}

// Default new a config with specified defualt value.
func Default() *Config {
	return &Config{
		RPCClient: &RPCClient{Dial: xtime.Duration(time.Second), Timeout: xtime.Duration(time.Second)},
		RPCServer: &RPCServer{
			Network:           "tcp",
			Addr:              "3119",
			Timeout:           xtime.Duration(time.Second),
			IdleTimeout:       xtime.Duration(time.Second * 60),
			MaxLifeTime:       xtime.Duration(time.Hour * 2),
			ForceCloseWait:    xtime.Duration(time.Second * 20),
			KeepAliveInterval: xtime.Duration(time.Second * 60),
			KeepAliveTimeout:  xtime.Duration(time.Second * 20),
		},
	}
}

// Config config.
type Config struct {
	RPCClient  *RPCClient
	RPCServer  *RPCServer

	Kafka      *Kafka
	Redis      *Redis
	Etcd   	   *Etcd
	Log      *Log
	Prometheus *Prometheus
	Telemetry  *Telemetry
}
// Redis .
type Redis struct {
	Network      string
	Addr         string
	Auth         string
	Active       int
	Idle         int
	DialTimeout  xtime.Duration
	ReadTimeout  xtime.Duration
	WriteTimeout xtime.Duration
	IdleTimeout  xtime.Duration
	Expire       xtime.Duration
}

// Kafka .
type Kafka struct {
	Topic   string
	Brokers []string
}
// Etcd .
type Etcd struct {
	Endpoints []string
}
// RPCClient is RPC client config.
type RPCClient struct {
	Dial    xtime.Duration
	Timeout xtime.Duration
}

// RPCServer is RPC server config.
type RPCServer struct {
	Name  string
	Network           string
	Addr              string
	Timeout           xtime.Duration
	IdleTimeout       xtime.Duration
	MaxLifeTime       xtime.Duration
	ForceCloseWait    xtime.Duration
	KeepAliveInterval xtime.Duration
	KeepAliveTimeout  xtime.Duration
}


// Log .
type Log struct {
	serviceName   string
	level   string
}
// Telemetry
type Telemetry struct {
	Name      	 string
	Endpoint     string
	Sampler      float64
	Batcher      string
}
// Prometheus
type Prometheus struct {
	Host      	 string
	Port      	 int
	Path         string
}
func Service(Conf *Config) *service.ServiceConf {
	sv := &service.ServiceConf{
		Name:       Conf.RPCServer.Name,
		Log:        logx.LogConf{
			ServiceName:         Conf.Log.serviceName,
			Mode:                "console",
			Encoding:            "json",
			TimeFormat:			 "",
			Path:                "",
			Level:               Conf.Log.level,
			Compress:            false,
			KeepDays:            0,
			StackCooldownMillis: 0,
		},
		Mode:       "pro",
		MetricsUrl: "",
		Prometheus: prometheus.Config{
			Host: Conf.Prometheus.Host,
			Port: Conf.Prometheus.Port,
			Path: Conf.Prometheus.Path,
		},
		Telemetry:  trace.Config{
			Name:     Conf.Telemetry.Name,
			Endpoint: Conf.Telemetry.Endpoint,
			Sampler:  Conf.Telemetry.Sampler,
			Batcher:  Conf.Telemetry.Batcher,
		},
	}
	return sv
}